
package com.rt.schedulenew.utils.base;
import org.slf4j.LoggerFactory;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
public class ShiroUtil
{
    private static Logger log=LoggerFactory.getLogger(ShiroUtil.class);;

    
    public static Object getCurrentUser() {
        Subject currentUser = SecurityUtils.getSubject();
        if (null != currentUser) {
            try {
                Session session = currentUser.getSession();
                if (null != session) {
                    return session.getAttribute("session.current.user");
                }
            }
            catch (InvalidSessionException e) {
                log.error(e.toString());
            }
        }
        return null;
    }
    
    public static void saveCurrentUser(Object user) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null) {
            Session session = currentUser.getSession();
            if (session != null) {
                session.setAttribute("session.current.user", user);
            }
        }
    }
    
    public static Session getSession() {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null) {
            return currentUser.getSession();
        }
        return null;
    }
    
    public static Boolean hasRole(String roleId) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null) {
            return currentUser.hasRole(roleId);
        }
        return false;
    }
    
    public static Boolean hasPermission(String permission) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser != null) {
            return currentUser.isPermitted(permission);
        }
        return false;
    }
}
